﻿using LitJson;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

public class Engine : Part
{
    public enum EngineType
    {
        None,
        ChemicalFuel,
        Electricity,
        Jet,
    }
    public EngineType engineType { get { return m_EngineType; } }
    float m_Power = 0;
    float m_PowerMax = 1;
    float m_Scale = 1;
    EngineType m_EngineType = EngineType.None;

    public float power
    {
        set { m_Power = value; }
        get { return m_Power; }
    }
    public float powerMax
    {
        set { m_PowerMax = value; }
        get { return m_PowerMax; }
    }

    public float scale
    {
        set { m_Scale = value; }
        get { return m_Scale; }
    }

    public enum ParameterType
    {
        NONE,
        Switch,
        Power,
        ParameterTypeEnd,
    }
    public Engine()
    {
        m_Type = ItemType.ENGINE;
    }
    public override void SetParameter(BehaviourMate behaviour)
    {
        switch ((ParameterType)behaviour.m_ID)
        {
            case ParameterType.Switch:
                isEnabled = behaviour.m_TargetData.m_TargetDataBool;
                break;
            case ParameterType.Power:
                power = behaviour.m_TargetData.m_TargetDataFloat;
                break;
        }
        return;
    }
    public override int GetParameterNumber()
    {
        return (int)ParameterType.ParameterTypeEnd;
    }

    public override string GetParameterName(int type)
    {
        string name = "无";
        switch ((ParameterType)type)
        {
            case ParameterType.Switch:
                name = "开关";
                break;
            case ParameterType.Power:
                name = "功率";
                break;
        }
        return name;
    }
    public override BehaviourMate GetDefaultInfoOfParameter(int type)
    {
        var name = GetParameterName(type);
        if (name == "无")
            return null;
        var commonData = new CommonData();
        BehaviourMate defaultInfo = new BehaviourMate(name, type, commonData);
        defaultInfo.m_ID = type;
        switch ((ParameterType)type)
        {
            case ParameterType.Switch:
                commonData.m_DataType = CommonData.DataType.Bool;
                break;
            case ParameterType.Power:
                commonData.m_DataType = CommonData.DataType.Float;
                defaultInfo.m_MaxValue = 1f;
                defaultInfo.m_MinValue = -1f;
                break;
        }
        return defaultInfo;
    }

    public override JsonData Save()
    {
        var root = base.Save();
        return root;
    }

    public override void LoadPrefab(JsonData json)
    {
        base.LoadPrefab(json);
        IDictionary<string, JsonData> jsond = json.ValueAsObject();
        if (jsond.ContainsKey("InitAngle"))
            m_InitAngle = jsond["InitAngle"].ValueAsInt();
        if (jsond.ContainsKey("EngineType"))
            m_EngineType = (EngineType)jsond["EngineType"].ValueAsInt();
        if (jsond.ContainsKey("PowerMax"))
            m_PowerMax = jsond["PowerMax"].ValueAsInt();
        if (jsond.ContainsKey("Scale"))
            m_Scale = (float)jsond["Scale"].ValueAsDouble();
    }
    public override bool Load(JsonData json)
    {
        base.Load(json);

        return true;
    }
    public override void Copy(Item item)
    {
        base.Copy(item);
        var engine = item as Engine;
        if (Utils.CheckReferenceIsNull(engine))
            return;
        m_InitAngle = engine.m_InitAngle;
        m_EngineType = engine.m_EngineType;
        m_PowerMax = engine.m_PowerMax;
        m_Scale = engine.m_Scale;
    }
}